package edu.bbu.commonproject.desktop.gui.view.swing.utils;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * The header button class.
 */
public class HeaderButton extends JButton
{
	/** It initializes the MOUSE_OVER. */
	private static final int MOUSE_OVER = 0;
	
	/** It initializes the MOUSE_EXIT. */
	private static final int MOUSE_EXIT = 1;
	
	/** It initializes the MOUSE_PRESS. */
	private static final int MOUSE_PRES = 2;
	
	/** The background color. */
	private Color backColor;
	
	/** The mouse over image. */
	private Image mouseOverImage;
	
	/** The mouse ppressed image. */
	private Image mousePressedImage;	
	
	/** It initializes the status.. */
	private int status = MOUSE_EXIT;
	
	/**
	 * The constructor.
	 */
	public HeaderButton()
	{
		this.setOpaque(false);
		this.setFocusable(false);
		this.setPreferredSize(new Dimension(80, 80));
		
		backColor = new Color(255, 255, 255, 0);
		
		mouseOverImage = Design.HEADER_BUTTON_MOUSEOVER;
		mousePressedImage = Design.HEADER_BUTTON_MOUSEPRESS;
		
		setListeners();
	}
	
	/**
	 * Sets up the mouse listeners.
	 */
	private void setListeners()
	{
		this.addMouseListener(new MouseAdapter()
		{
			public void mouseEntered(MouseEvent e)
			{
				status = MOUSE_OVER;
				repaint();
			}
			
			public void mouseExited(MouseEvent e)
			{
				status = MOUSE_EXIT;
				repaint();
			}
			
			public void mousePressed(MouseEvent e)
			{
				status = MOUSE_PRES;
				repaint();
			}
			
			public void mouseReleased(MouseEvent e)
			{
				status = MOUSE_OVER;
				repaint();
			}
		});
	}
	
	/**
	 * The paint component.
	 */
	protected void paintComponent(Graphics g)
	{
		switch (status)
		{
			case MOUSE_EXIT:
			{
				g.setColor(backColor);
				g.fillRect(0, 0, getWidth(), getHeight());
				
			}break;
			
			case MOUSE_OVER:
			{
				g.drawImage(mouseOverImage, 0, 0, null);
				
			}break;
			
			case MOUSE_PRES:
			{
				g.drawImage(mousePressedImage, 0, 0, null);
				
			}break;
		}
		
		Icon imageIcon = this.getIcon();
		
		if(imageIcon != null)
		{
			int iconX = (this.getWidth() - imageIcon.getIconWidth()) / 2;
			int iconY = (this.getHeight() - imageIcon.getIconHeight()) / 2;
			
			imageIcon.paintIcon(this, g, iconX, iconY);
		}
	}
	
}
